﻿package com.sgcc.nfc.lock.presentation.users

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.sgcc.nfc.lock.R
import com.sgcc.nfc.lock.databinding.ItemUserBinding
import com.sgcc.nfc.lock.domain.model.RoleType
import com.sgcc.nfc.lock.domain.model.UserProfile

class UserAdapter(
    private val onItemClick: (UserProfile) -> Unit,
    private val onResetPassword: (UserProfile) -> Unit,
    private val onDelete: (UserProfile) -> Unit
) : ListAdapter<UserProfile, UserAdapter.UserViewHolder>(DiffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
        val binding = ItemUserBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return UserViewHolder(binding)
    }

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class UserViewHolder(
        private val binding: ItemUserBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: UserProfile) {
            val context = binding.root.context
            binding.userNameText.text = item.realName.takeIf { it.isNotBlank() } ?: item.username
            binding.userRoleChip.text = resolveRoleLabel(context, item)

            val regionLabel = item.regionName ?: context.getString(R.string.user_region_unknown)
            binding.userRegionText.text = regionLabel
            binding.userRegionText.isVisible = regionLabel.isNotBlank()

            val phone = item.phone
            if (phone.isNullOrBlank()) {
                binding.userPhoneText.isVisible = false
            } else {
                binding.userPhoneText.isVisible = true
                binding.userPhoneText.text = context.getString(R.string.user_phone_label, phone)
            }

            binding.root.setOnClickListener { onItemClick(item) }
            binding.resetButton.setOnClickListener { onResetPassword(item) }
            binding.deleteButton.setOnClickListener { onDelete(item) }
        }
    }

    companion object DiffCallback : DiffUtil.ItemCallback<UserProfile>() {
        override fun areItemsTheSame(oldItem: UserProfile, newItem: UserProfile): Boolean = oldItem.userId == newItem.userId
        override fun areContentsTheSame(oldItem: UserProfile, newItem: UserProfile): Boolean = oldItem == newItem
    }

    private fun resolveRoleLabel(context: Context, user: UserProfile): String {
        user.roleName?.takeIf { it.isNotBlank() }?.let { return it }
        return when (RoleType.fromValue(user.roleType)) {
            RoleType.SUPER_ADMIN -> context.getString(R.string.user_role_super_admin)
            RoleType.PROVINCE_ADMIN -> context.getString(R.string.user_role_province_admin)
            RoleType.CITY_ADMIN -> context.getString(R.string.user_role_city_admin)
            RoleType.COUNTY_ADMIN -> context.getString(R.string.user_role_county_admin)
            RoleType.OPERATIONS_CENTER_ADMIN -> context.getString(R.string.user_role_center_admin)
            RoleType.STATION_ADMIN -> context.getString(R.string.user_role_station_admin)
            RoleType.OPERATOR -> context.getString(R.string.user_role_operator)
            null -> context.getString(R.string.user_form_role_unknown)
        }
    }
}

